//
//  TCClothesViewController.m
//  Project_B
//
//  Created by dllo on 15/11/27.
//  Copyright © 2015年 张闯. All rights reserved.
//

#import "TCClothesViewController.h"
#import "AFNetworkHandler.h"
#import "UIImageView+WebCache.h"
#import "MBProgressHUD.h"
#import "TCClothesCollectionViewCell.h"
#import "Clothes.h"
#import "TCClothesDetailViewController.h"
#import "RESideMenu.h"
#import "UIImage+GIF.h"
#define WIDTH [[UIScreen mainScreen] bounds].size.width / 375
#define HEIGHT [[UIScreen mainScreen] bounds].size.height / 667
@interface TCClothesViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, strong)NSMutableArray *dataArr;
@property (nonatomic, strong)MBProgressHUD *HUD;
@end

@implementation TCClothesViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.dataArr = [NSMutableArray array];
//    self.navigationController.navigationBar.translucent = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-fanhui@2x"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    
    [self creatCollection];
    [self getData];
}
- (void)getData
{
    [AFNetworkHandler getUrl:self.detailID cookie:nil result:^(id result) {
        [self.dataArr addObjectsFromArray:[Clothes transformWithArray:result[@"data"]]];
        [self.HUD hide:YES];
        [self.collectionView reloadData];
    }];
}
- (void)creatCollection
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake((355/2-20)*WIDTH, 280*HEIGHT);
    layout.minimumInteritemSpacing = 20;
    layout.minimumLineSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(15, 15, 15, 15);
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, 375*WIDTH, 667*HEIGHT) collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:self.collectionView];
    self.collectionView.backgroundColor = [UIColor colorWithRed:245.0/255.0 green:240.0/255.0 blue:231.0/255.0 alpha:1];
    [self.collectionView registerClass:[TCClothesCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
   self.HUD.mode = MBProgressHUDModeIndeterminate;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TCClothesCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.temp = self.dataArr[indexPath.item];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    TCClothesDetailViewController *secVC = [[TCClothesDetailViewController alloc]init];
    secVC.temp = self.dataArr[indexPath.item];
    [self.navigationController pushViewController:secVC animated:YES];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

- (void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
